<?php
namespace app\protocol\commands;

use app\protocol\CommandInterface;
use Workerman\Connection\TcpConnection;

abstract class BaseCommand implements CommandInterface
{
    /**
     * 验证参数数量
     */
    protected function validateArgumentCount(array $args, int $expected, string $usage = ''): void
    {
        $count = count($args);
        if ($count !== $expected) {
            throw new \InvalidArgumentException(
                "参数数量错误，期望 {$expected} 个，收到 {$count} 个。使用: " . $usage
            );
        }
    }

    /**
     * 验证数字参数
     */
    protected function validateNumeric($value, string $paramName = '参数'): float
    {
        if (!is_numeric($value)) {
            throw new \InvalidArgumentException("{$paramName} 必须是数字: '{$value}'");
        }
        return floatval($value);
    }

    /**
     * 解析参数字符串为数组
     */
    protected function parseParams(string $params): array
    {
        return preg_split('/\s+/', trim($params));
    }

    /**
     * 默认描述
     */
    public static function getDescription(): string
    {
        return '基础命令';
    }

    /**
     * 默认用法
     */
    public static function getUsage(): string
    {
        return 'command <params>';
    }
}